//
//  BaseViewController.swift
//  shaohuo
//
//  Created by User on 15/12/28.
//  Copyright © 2015年 lchenc3. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController {
    
    var locationManager : BMKLocationManager?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
            //百度地图
            BMKLocationAuth.sharedInstance().checkPermision(withKey: "MXZbCfB1zphE7ThZESkGdEpSmNI7DCjO", authDelegate: self)
            
            initBMKLocation()
        
    }
    
    func initBMKLocation() -> Void {
        
        //初始化实例
        locationManager = BMKLocationManager()
        //设置delegate
        locationManager?.delegate = self
        //设置返回位置的坐标系类型
        locationManager?.coordinateType = .BMK09LL
        //设置距离过滤参数
        locationManager?.distanceFilter = kCLDistanceFilterNone
        //设置预期精度参数
        locationManager?.desiredAccuracy = kCLLocationAccuracyBest
        //设置应用位置类型
        locationManager?.activityType = .automotiveNavigation
        //设置是否自动停止位置更新
        locationManager?.pausesLocationUpdatesAutomatically = false
        //设置是否允许后台定位
//        locationManager?.allowsBackgroundLocationUpdates = true
        //设置位置获取超时时间
        locationManager?.locationTimeout = 10
       //设置获取地址信息超时时间
        locationManager?.reGeocodeTimeout = 10
        //开启定位
        locationManager?.startUpdatingLocation()
    }
    
    
    var timeTicket: String  {
        get {
            return "\(Int(Date().timeIntervalSince1970))"
        }
    }
    
    //保存经纬度
    func requestSaveLocate(lat:String,lng:String) -> Void {
        
        if !UserTicketModel.sharedInstance.isLogin {
            return
        }
        
        //要加密的数据
        let param = ["command":"saveLocate","uid":UserTicketModel.sharedInstance.uid,"token":UserTicketModel.sharedInstance.token,"app_name":"shr","lat":lat,"lng":lng]
        
        NetworkTools.sharedInstance.postRequest(urlString: "", parameters: param as AnyObject, headers: ["copyright":"v1","interface":"Shipping"], successed: { (resultDict) in
            
//            let data = resultDict["data"] as! [String:AnyObject]
            print("经纬度提交成功")

        })
    }

}

extension BaseViewController : BMKLocationAuthDelegate,BMKLocationManagerDelegate{

    func bmkLocationManager(_ manager: BMKLocationManager, didUpdate location: BMKLocation?, orError error: Error?) {

        if location != nil {
            if location?.location != nil {
                UserTicketModel.sharedInstance.lat = "\(Double((location?.location?.coordinate.latitude)!))"
                UserTicketModel.sharedInstance.lng = "\(Double((location?.location?.coordinate.longitude)!))"
                requestSaveLocate(lat: "\(Double((location?.location?.coordinate.latitude)!))", lng: "\(Double((location?.location?.coordinate.longitude)!))")
            }
        }
    }
}


